package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.OilCard;
import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.OilCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/oilCard")
public class OilCardController {

    @Autowired
    OilCardService oilCardService;

    @PostMapping
    public Return save(OilCard oilCard){
        Return ret=new Return();
        if(oilCardService.insert(oilCard)>0);
        ret.getCommon().setRet(true);
        return ret;
    }

    @GetMapping(value = "/byCardNo/{cardNo}")
    public Return<OilCard> queryByCardNo(@PathVariable(value = "cardNo")String cardNo){
        Return<OilCard> ret=new Return<>();
        OilCard oilCard=oilCardService.queryByCardNo(cardNo);
        if(oilCard!=null) {
            ret.getCommon().setRet(true);
            ret.setData(oilCard);
        }
        return ret;
    }
    @GetMapping
    public Return<List<OilCard>> query(PageInfo info){
        Return ret=new Return();
        List<OilCard> oilCards=oilCardService.query();
        if(!CollectionUtils.isEmpty(oilCards)){
            ret.getCommon().setRet(true);
            ret.setData(oilCards);
        }
        return ret;
    }

    @DeleteMapping(value = "/{oilCardId}")
    public Return delete(@PathVariable(value = "oilCardId")String oilCardId){
        Return ret=new Return();
        if(oilCardService.delete(oilCardId)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @PutMapping
    public Return update(OilCard oilCard){
        Return ret=new Return();
        if(oilCardService.update(oilCard)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }






}
